<template>
  <div>
    <div class="flex justify-between items-center mt-2">
      <a-radio-group v-model:value="isDisplayed" button-style="solid">
        <a-radio-button value="0">本次</a-radio-button>
        <a-radio-button value="">全部</a-radio-button>
      </a-radio-group>
      <div>
        <a-button type="primary" :disabled="store.radioKey" class="mr-2" @click="onSign()">签署</a-button>
        <a-button type="primary" :disabled="store.radioKey" class="mr-2" @click="onClickAdd()">新增申请单</a-button>
      </div>
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printMe" v-print="{ id: 'printMe' }">打印</a-button>
        <PrintView id="printMe" :print-data="printData" />
      </div>
    </div>
    <template v-if="applyList.length">
      <div style="height: calc(100vh - 260px)" class="mt-2 px-1.5 overflow-auto">
        <div v-for="(item, index) in applyList" :key="index" class="mt-5 mb-2 border border-solid rounded-sm">
          <div class="h-12 border-b border-solid flex items-center bg-thin">
            <div class="name truncate">
              <LabelValue class="ml-10" :width="240" tooltip :value="item?.categroyName" />
            </div>
            <div>
              <a-tag v-if="item.status === '0'" color="blue">已开立</a-tag>
              <a-tag v-if="item.status === '1'" color="green">已审核</a-tag>
              <a-tag v-if="item.status === '2'" color="red">已作废</a-tag>
              <a-tag v-if="item.status === '3'" color="green">已签署</a-tag>
            </div>
            <LabelValue class="ml-12" label="科室" tooltip :width="180" :value="item?.applyDeptName" />
            <LabelValue class="ml-1" label="申请医生" tooltip :width="150" :value="item?.applyDoctorName" />
            <LabelValue class="ml-12" label="申请时间" tooltip :width="227" :value="item?.applyTime" />
          </div>
          <div class="h-16 flex items-center justify-between">
            <div class="flex items-center">
              <LabelValue class="ml-12 mr-8" :width="462" label="" tooltip :value="item.showTitle" />
              <LabelValue class="ml-12" label="审核人" tooltip :width="150" :value="item?.lisRecordEntity?.auditName" />
              <LabelValue class="ml-12" label="审核时间" tooltip :width="200" :value="item?.lisRecordEntity?.auditTime" />
            </div>
            <div class="pr-4 w-[200px] text-center">
              <BaseButton
                v-if="item.status === '3'"
                confirm-text="确认撤签?"
                type="text"
                :disabled="store.radioKey"
                danger
                size="small"
                @click="onUnSign(item)"
              >
                撤签
              </BaseButton>
              <template v-if="item.status === '0'">
                <BaseButton type="link" size="small" :disabled="store.radioKey" @click="onEdit(item)"> 修改 </BaseButton>
                <BaseButton confirm-text="确认作废?" :disabled="store.radioKey" type="text" danger size="small" @click="onDelete(item)"> 作废 </BaseButton>
              </template>
              <template v-if="item.status === '1'">
                <a-button type="link" size="small" @click="onPrint(item, 1)"> 查阅报告 </a-button>
                <a-button type="link" size="small" @click="onPrint(item, 2)"> 打印 </a-button>
              </template>
            </div>
          </div>
        </div>
      </div>
    </template>
    <a-empty v-else-if="!loading" />
    <div v-if="loading" class="w-full text-center pt-20">
      <a-spin :spinning="loading" />
    </div>
  </div>
  <Edit v-model:open="open" :row-data="currentRow" @success="queryList()" />
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import LabelValue from '@/components/LabelValue'

// import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../../store'
import Edit from './Edit'
import PrintView from './PrintView.vue'

const store = useStore()
const open = ref(false)
const loading = ref(false)
const applyList = ref([])
const isDisplayed = ref('0')
const printData = ref({})
const printMe = ref()

const onClickAdd = () => {
  open.value = true
}

const queryList = async () => {
  loading.value = true
  const { code, data, msg } = await post('/lis/LisApply/getLisApplyList', {
    pyjm: isDisplayed.value ? '' : undefined,
    applyPath: isDisplayed.value === '0' ? 1 : undefined,
    key: isDisplayed.value === '0' ? store.currentPatient.inplsh : undefined,
    brId: isDisplayed.value !== '0' ? store.currentPatient.brId : undefined,
    isDisplayed: ''
  })
  loading.value = false
  if (code !== 0) return message.error(msg || '请求失败')
  applyList.value = data
  applyList.value.forEach((i) => {
    i.showTitle = [...new Set(i.lisApplyItemEntityList.map((item) => item.lisSetName || item.orderItemName))].join(',')
  })
}

const onDelete = async (row) => {
  const { code, msg } = await post('/lis/LisApply/doCancelLisApply', { lsh: row.lsh })
  if (code !== 0) return message.error(msg)
  message.success('作废成功')
  queryList()
}
//签署
const onSign = async () => {
  const ids = []
  applyList.value.forEach((item) => {
    if (item.status === '0') {
      item.lisApplyItemEntityList.forEach((item) => {
        ids.push(item.key)
      })
    }
  })
  if (ids.length === 0) {
    return message.error('无项目')
  }
  const { code, msg } = await post('/inp/clinicalOrder/signedDoctor', { inpAdvIds: ids, signStatus: 1 })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success('操作成功')
  queryList()
}
//撤签
const onUnSign = async (row = {}) => {
  const ids = []
  row.lisApplyItemEntityList.forEach((item) => {
    ids.push(item.key)
  })
  if (ids.length === 0) {
    return message.error('无项目')
  }
  const { code, msg } = await post('/inp/clinicalOrder/signedDoctor', { inpAdvIds: ids, signStatus: 0 })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success('操作成功')
  queryList()
}
const currentRow = ref({})
const onEdit = async (row) => {
  currentRow.value = row
  open.value = true
}

// 打印单据
const onPrint = async (item, dylx) => {
  // /lis/lisPrint/printLisApply
  const { code, msg, data } = await post('/lis/lisPrint/printLisRecord', { lsh: item.lsh, dylx })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
  // if (getSystemConfig('NAVYHIS_MODE') === '1') {
  //   printData.value = JSON.parse(data)
  //   setTimeout(() => {
  //     printMe.value.$el.click()
  //   }, 1000)
  // } else {

  // }
}

watchEffect(() => {
  if (store.currentPatient.inplsh) {
    queryList()
  }
})
</script>
<style lang="less" scoped>
.name {
  padding: 0 5px;
  font-weight: 700;
  font-size: 20px;
}
</style>
